package com.jxb.jingchen.print;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * @author jinxb
 * date     : 2021/8/19
 * desc     :
 */
public class PrinterAdapter extends RecyclerView.Adapter<PrinterAdapter.ViewHolder> {

    private Context mContext;
    private List<BluetoothDevice> mData;
    private OnItemClickListener onItemClickListener;

    public PrinterAdapter(Context mContext, List<BluetoothDevice> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_printer, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mTvPrinter.setText(mData.get(position).getName());
        holder.mTvPrinter.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(mData.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvPrinter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvPrinter = itemView.findViewById(R.id.tv_printer);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface  OnItemClickListener {
        void onItemClick(BluetoothDevice device);
    }
}
